package com.leoandroid.tool.toolsbox.camera.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件读取工具类
 */
public class FileUtil {

    public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getPath() + "/";
    public static final int MAX_DATA_BYTES = 10240;

    /**
     * 读取文件内容，作为字符串返回
     */
    public static String readFileAsString(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        }

        if (file.length() > 1024 * 1024 * 1024) {
            throw new IOException("File is too large");
        }

        StringBuilder sb = new StringBuilder((int) (file.length()));
        // 创建字节输入流  
        FileInputStream fis = new FileInputStream(filePath);
        // 创建一个长度为10240的Buffer
        byte[] bbuf = new byte[10240];
        // 用于保存实际读取的字节数  
        int hasRead = 0;
        while ((hasRead = fis.read(bbuf)) > 0) {
            sb.append(new String(bbuf, 0, hasRead));
        }
        fis.close();
        return sb.toString();
    }

    /**
     * 根据文件路径读取byte[] 数组
     */
    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;
            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                return bos.toByteArray();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

                bos.close();
            }
        }
    }

    public static String bitmapCompress(String str) throws FileNotFoundException {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inSampleSize = 4;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap decodeFile = BitmapFactory.decodeFile(str, options);
        String str2 = ROOT_PATH + System.currentTimeMillis() + ".png";
        boolean r4 = compress(decodeFile, str2);
        StringBuilder sb = new StringBuilder();
        sb.append("质量压缩 size--->:byte:");
        sb.append(fileAvailable(str2));
        sb.append("    kb:");
        sb.append(((float) fileAvailable(str2)) / 1024.0f);
        return r4 ? str2 : "";
    }

    public static boolean compress(Bitmap bitmap, String str) throws FileNotFoundException {
        return bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(str));
    }

    public static int fileAvailable(String str) {
        try {
            return new FileInputStream(str).available();
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
